<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>MongoDBRef 类</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongoint64.tostring.html">MongoInt64::__toString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodbref.create.html">MongoDBRef::create</a></div>
 <div class="up"><a href="mongo.types.html">Types</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="class.mongodbref" class="reference">

 <h1 class="title">MongoDBRef 类</h1>
 

 <div class="partintro"><p class="verinfo">(PECL mongo &gt;=0.9.0)</p>


  <div class="section" id="mongodbref.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    这个类可以用于创建不同集合中对象间的轻量级的链接。
   </p>
   <p class="para">
    <em class="emphasis">Motivation</em>：如果我们需要引用其他集合中的文档。最简单的方法是在当前文档中创建一个字段。比如，有
    &quot;people&quot; 集合和 &quot;addresses&quot; 集合，我们需要“关联”每个 person 和对应的 address ，可以：
    <div class="example" id="example-1470">
     <p><strong>Example #1 链接文档</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$people&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">people</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$addresses&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addresses</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$myAddress&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"line&nbsp;1"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"123&nbsp;Main&nbsp;Street"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"line&nbsp;2"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"city"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Springfield"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"state"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Vermont"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"country"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"USA"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;save&nbsp;the&nbsp;address&nbsp;保存address文档<br /></span><span style="color: #0000BB">$addresses</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$myAddress</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;save&nbsp;a&nbsp;person&nbsp;with&nbsp;a&nbsp;reference&nbsp;to&nbsp;the&nbsp;address&nbsp;保存一个people，关联刚才的address<br /></span><span style="color: #0000BB">$me&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Fred"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"address"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$myAddress</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$me</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    然后，我们可以：把保存在 &quot;people&quot; 集合中的
    <a href="class.mongoid.html" class="classname">MongoId</a> 作为条件，查询
    &quot;address&quot; 集合，来获取一个人的地址。
   </p>
   <p class="para">
    如果我们现在有更加一般性的的情况，我们不知道哪个集合（甚至数据库）中包含我们要引用的文档。
    <strong class="classname">MongoDBRef</strong> 就是个好选择，它是一个更加通用的格式，所有的驱动和数据库都可以处理它。
   </p>
   <p class="para">
    如果每个“人”有一系列关联到其他多个集合的信息，例如爱好、运动、书籍等，我们可以用数个
    <strong class="classname">MongoDBRef</strong> 对象来跟踪每一个 爱好 来自哪个集合：
    <div class="example" id="example-1471">
     <p><strong>Example #2 Creating MongoDBRef links</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$people&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"people"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;model&nbsp;trains&nbsp;are&nbsp;in&nbsp;the&nbsp;"hobbies"&nbsp;collection<br /></span><span style="color: #0000BB">$trainRef&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDBRef</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"hobbies"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$modelTrains</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">]);<br /></span><span style="color: #FF8000">//&nbsp;soccer&nbsp;is&nbsp;in&nbsp;the&nbsp;"sports"&nbsp;collection<br /></span><span style="color: #0000BB">$soccerRef&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDBRef</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"sports"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$soccer</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;now&nbsp;we'll&nbsp;know&nbsp;what&nbsp;collections&nbsp;the&nbsp;items&nbsp;in&nbsp;the&nbsp;"likes"&nbsp;array&nbsp;came&nbsp;from&nbsp;when<br />//&nbsp;we&nbsp;retrieve&nbsp;this&nbsp;document<br />//&nbsp;&nbsp;#&nbsp;现在当我们读取这个文档的时候，就可以知道“likes”字段里的数组元素分别来自哪个集合了。<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Fred"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"likes"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #0000BB">$trainRef</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$soccerRef</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    数据库引用可以被理解为超链接：它们指定了一个文档的唯一地址，但不会自动读取或者跟踪引用、链接。、
   </p>
   <p class="para">
    一个数据库引用仅仅是一个普通关联数组，不是
    <strong class="classname">MongoDBRef</strong> 的实例，所以这个类与其他数据类型有些不同。这个类只包含静态方法，用来操作引用。
    译注：上面两段的意思概括为
    1.一个数据库引用与超链接相似，复制、删除、修改等操作不会影响原来的文档。
    2.读取这个引用可以得知指向的文档的位置，但不能知道文档的内容，要手动解引用。
    3.这个“引用”保存到Mongo的时候就是普通数组
   </p>
  </div>


  <div class="section" id="mongodbref.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <strong class="classname">MongoDBRef</strong>
     </span>
     {</div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">array</span><span class="methodname"><a href="mongodbref.create.html" class="methodname">create</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$collection</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$id</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$database</code></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">array</span><span class="methodname"><a href="mongodbref.get.html" class="methodname">get</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.mongodb.html" class="type MongoDB">MongoDB</a></span> <code class="parameter">$db</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$ref</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">bool</span><span class="methodname"><a href="mongodbref.isref.html" class="methodname">isRef</a></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$ref</code></span>
   )</div>

   }</div>


  </div>

  <div class="section">
   <h2 class="title">参见</h2>
   <p class="para">
    MongoDB 官方核心文档 <a href="http://docs.mongodb.org/manual/reference/database-references/" class="link external">&raquo;&nbsp;databases references</a>.
   </p>
  </div>
 </div>

 





 







 







 



<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="mongodbref.create.html">MongoDBRef::create</a> — 创建一个新的数据库引用</li><li><a href="mongodbref.get.html">MongoDBRef::get</a> — 获取引用所指向的对象</li><li><a href="mongodbref.isref.html">MongoDBRef::isRef</a> — 检测数组是否为数据库引用</li></ul>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongoint64.tostring.html">MongoInt64::__toString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodbref.create.html">MongoDBRef::create</a></div>
 <div class="up"><a href="mongo.types.html">Types</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
